package logica.email;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
 * Classe principal para gerencia o envio de email.
 * 
 */
public class MailManager {

	private final String usuario = "leilaosi2008.2";
	private final String senha = "Lzed@sys.1680";

	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

	/**
	 * Contrutor da classe.
	 */
	public MailManager() {
	}

	/**
	 * metodo para envio do email.
	 * 
	 * @param msg
	 * 			mensagen a ser enviado.
	 * @throws Exception
	 * 			excessao gerada caso o email nao consiga ser enviado.
	 */
	public void send(MailMessage msg) throws Exception {
		try {
			Properties mailProps = new Properties();
			mailProps.put("mail.smtp.host", SMTP_HOST_NAME);

			SimpleAuth auth = null;
			auth = new SimpleAuth(usuario, senha);

			mailProps.put("mail.smtp.auth", "true");
			mailProps.put("mail.user", usuario);
			mailProps.put("mail.from", msg.getFrom());
			mailProps.put("mail.to", msg.getTo());

			mailProps.put("mail.smtp.host", SMTP_HOST_NAME);
			mailProps.put("mail.smtp.auth", "true");
			mailProps.put("mail.debug", "true");

			mailProps.put("mail.smtp.port", SMTP_PORT);
			mailProps.put("mail.smtp.socketFactory.port", SMTP_PORT);
			mailProps.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			mailProps.put("mail.smtp.socketFactory.fallback", "false");

			Session mailSession = Session.getDefaultInstance(mailProps, auth);

			mailSession.setDebug(true);

			Message email = new MimeMessage(mailSession);
			email.setRecipients(Message.RecipientType.TO, InternetAddress
					.parse(msg.getTo()));
			if (!msg.getCc().trim().equals("")) {
				email.setRecipients(Message.RecipientType.CC, InternetAddress
						.parse(msg.getCc()));
			}
			if (!msg.getBcc().trim().equals("")) {
				email.setRecipients(Message.RecipientType.BCC, InternetAddress
						.parse(msg.getBcc()));
			}
			email.setFrom(new InternetAddress(msg.getFrom()));
			email.setSubject(msg.getSubject());
			email.setContent(msg.getText(), "text/plain");

			Transport tr = mailSession.getTransport("smtp");
			tr.connect(SMTP_HOST_NAME, usuario, senha);
			email.saveChanges(); // don't forget this
			tr.sendMessage(email, email.getAllRecipients());
			tr.close();

		} catch (Exception e) {
			System.out.println(" [E] ERROR: " + e);
			e.printStackTrace(System.out);
			throw e;
		}
		System.out.println(" [#] Email successfully sent");
		return;
	}

	/**
	 * metodo de envio de emal.
	 * 
	 * @param to
	 * @param cc
	 * @param bcc
	 * @param from
	 * @param subject
	 * @param text
	 * @throws Exception
	 */
	public void send(String to, String cc, String bcc, String from,
			String subject, String text) throws Exception {
		MailMessage msg = new MailMessage(to, cc, bcc, from, subject, text);
		this.send(msg);
		return;
	}
}
